package com.zzp.zjs_bos.service.impl;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.zzp.zjs_bos.dao.IUserDao;
import com.zzp.zjs_bos.domain.Role;
import com.zzp.zjs_bos.domain.User;
import com.zzp.zjs_bos.service.IUserService;
import com.zzp.zjs_bos.utils.MD5Utils;

@Service
@Transactional
public class UserServiceImpl implements IUserService {

	@Resource
	private IUserDao userDao;
	
	public User login(User model) {
		String password = MD5Utils.md5(model.getPassword());
		List<User> list = userDao.findByNameQuery("findByUserNameAndPassword",model.getUsername(),password );
		if(list.size()>0 && list != null){
			return list.get(0);
		}
		return null;
	}

	public void editPassword(String id, String password) {
		userDao.executeNamedQuery("editPassword",password,id);
	}


	public void save(User model, String[] roleIds) {
		userDao.save(model);
		model.setPassword(MD5Utils.md5(model.getPassword()));
		for (String id : roleIds) {
			Role role = new Role(id);
			model.getRoles().add(role);
		}
	}

	public List<User> findAll() {
		return userDao.findAll();
	}

	public User findUser(User model) {
		return userDao.findByUserName(model.getUsername());
	}
}


